//
//  ClubScanLoginViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/23.
//

import UIKit

class ClubScanLoginViewController: ClubBaseViewController {

    var closeButton:UIButton!
    var centerImageView:UIImageView!
    var centerLabel:UILabel!
    var loginButton:UIButton!
    var cancelButton:UIButton!
    var strScanned = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
    }
    
    private func initUI() -> Void {

        closeButton = UIButton(type: .custom)
        closeButton.setTitleColor("#888888".toColor, for: .normal)
        closeButton.setTitle("关闭", for: .normal)
        closeButton.titleLabel?.font = .systemFont(ofSize: 14.0.Scale)
        closeButton.addTarget(self, action: #selector(closeButtonClick(_:)), for: .touchUpInside)
        view.addSubview(closeButton)
        
        centerImageView = UIImageView()
        centerImageView.contentMode = .scaleAspectFill
        centerImageView.image = UIImage(named: "scanLoginImage")
        view.addSubview(centerImageView)
        
        centerLabel = UILabel()
        centerLabel.text = "网页登录确认"
        centerLabel.font = 16.font_semiBold
        centerLabel.textColor = .black
        view.addSubview(centerLabel)
        
        loginButton = UIButton(type: .custom)
        loginButton.titleLabel?.font = .boldSystemFont(ofSize: 16.0.Scale)
        loginButton.frame = CGRect(x: 0, y: 0, width: 303.0.Scale, height: 48.0.Scale)
        loginButton.layer.cornerRadius = 24.0.Scale
        loginButton.layer.masksToBounds = true
        loginButton.setTitle("登录", for: .normal)
        loginButton.addTarget(self, action: #selector(loginButtonClick(_:)), for: .touchUpInside)
        view.addSubview(loginButton)
        loginButton.rae.gradientColors(.leftToRight(["#FA8C08".toColor,"#FDC013".toColor]))
        
        cancelButton = UIButton(type: .custom)
        cancelButton.setTitle("取消登录", for: .normal)
        cancelButton.setTitleColor("#888888".toColor, for: .normal)
        cancelButton.titleLabel?.font = .systemFont(ofSize: 16.0.Scale)
        cancelButton.addTarget(self, action: #selector(closeButtonClick(_:)), for: .touchUpInside)
        view.addSubview(cancelButton)
    }
    
    private func initFrame() -> Void {
        closeButton.snp.makeConstraints { make in
            make.right.equalTo(-18.0.Scale)
            make.top.equalTo(58.0.Scale)
            make.height.equalTo(20.0.Scale)
        }
        
        centerImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(192.0.Scale)
            make.width.height.equalTo(109.0.Scale)
        }
        
        centerLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(centerImageView.snp.bottom).offset(18.0.Scale)
            make.height.equalTo(23.0.Scale)
        }
        
        loginButton.snp.makeConstraints { make in
            make.left.equalTo(36.0.Scale)
            make.right.equalTo(-36.0.Scale)
            make.height.equalTo(48.0.Scale)
            make.top.equalTo(centerLabel.snp.bottom).offset(249.0.Scale)
        }
        
        cancelButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(loginButton.snp.bottom).offset(31.0.Scale)
            make.height.equalTo(23.0.Scale)
        }
    }
    
    @objc func closeButtonClick(_:UIButton) -> Void {
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc func loginButtonClick(_:UIButton) -> Void {
        Task{
            
            let res = await network.requestSpecial(.scanLogin(token: strScanned), isShowError: false)
            if res.isSuccess {
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
}
